package com.vhall.component.service.subject.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vhall.component.dao.subject.SubjectMapper;
import com.vhall.component.dao.subject.SubjectRoomMapper;
import com.vhall.component.entity.data.vo.BaseDataVO;
import com.vhall.component.entity.data.vo.BasicStatisticsInfoVO;
import com.vhall.component.entity.data.vo.UserParamVO;
import com.vhall.component.entity.subject.SubjectEntity;
import com.vhall.component.entity.subject.vo.SubjectBasicReqVo;
import com.vhall.component.entity.subject.vo.SubjectRoomLandRespVo;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.constant.ConstStr;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.data.DataService;
import com.vhall.component.service.subject.SubjectApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 会议专题服务实现类
 *
 * @author jianyang
 */
@Service
@Slf4j
public class SubjectApiServiceImpl implements SubjectApiService {

    @Autowired
    SubjectMapper subjectMapper;

    @Autowired
    SubjectRoomMapper subjectRoomMapper;

    @Autowired
    DataService dataService;


    @Override
    public JSONObject getLandingPageInfo(SubjectBasicReqVo reqVo) {
        SubjectEntity subjectEntity = subjectMapper.selectById(reqVo.getSubjectId());
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(subjectEntity));
        List<SubjectRoomLandRespVo> roomInfos = subjectRoomMapper.selectListBySubjectId(reqVo.getSubjectId());
        int uv = 0;
        int pv = 0;
        for (SubjectRoomLandRespVo roomInfo : roomInfos) {
            UserParamVO userParamVO = new UserParamVO();
            userParamVO.setRoomId(roomInfo.getRoomId());
            userParamVO.setIlId(roomInfo.getIlId());
            userParamVO.setStartTime(ConstStr.DATE_TIME_1970);
            userParamVO.setStatus("1");
            String now = LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_PATTERN);
            userParamVO.setEndTime(now);
            BasicStatisticsInfoVO basicStatisticsInfo = dataService.getBasicStatisticsInfo(userParamVO);
            if (basicStatisticsInfo != null) {
                BaseDataVO liveUv = basicStatisticsInfo.getLiveUv();
                Integer stat = liveUv.getStat();
                uv += stat;
                BaseDataVO livePv = basicStatisticsInfo.getLivePv();
                Integer pvStat = livePv.getStat();
                pv += pvStat;
                roomInfo.setUv(stat);
                roomInfo.setPv(pvStat);
            } else {
                log.info("获取房间观看信息失败");
                roomInfo.setUv(0);
                roomInfo.setPv(0);
            }
        }
        jsonObject.put("room_info", roomInfos);
        jsonObject.put("uv", uv);
        jsonObject.put("pv", pv);
        return jsonObject;
    }

    /**
     * 获取会议主题
     *
     * @param subjectId 主题id
     * @param accountId
     * @return
     */
    SubjectEntity checkSubjectStatus(Integer subjectId, Integer accountId) {
        LambdaQueryWrapper<SubjectEntity> eq = new LambdaQueryWrapper<SubjectEntity>().eq(SubjectEntity::getId, subjectId)
                .eq(SubjectEntity::getAccountId, accountId).eq(SubjectEntity::getSubjectStatus, 1);
        SubjectEntity subjectEntity = subjectMapper.selectOne(eq);
        if (subjectEntity == null) {
            throw new BusinessException(BizErrorCode.COMP_SUBJECT_NO_EXIST);
        }
        return subjectEntity;
    }

}
